#ifndef DECORE_REFPTR_H
#define DECORE_REFPTR_H

template <class T> 
class RefPtr
{
private:
	T*		mRef;
	int*	mComptor;

public:
	RefPtr(){mRef = 0; mComptor = new int(1);}
	RefPtr(T* pData);
	RefPtr(const RefPtr<T>& pPtr);

	~RefPtr();

	T* getRef(){ return mRef; }

	RefPtr& operator=(const RefPtr<T>& pPtr);
	RefPtr& operator=(T* pData);

	bool	operator==(T* pData) { if(mRef == pData) return true; return false;}
	bool	operator==(const RefPtr<T>& pPtr){ if(mRef == pPtr.mRef) return true; return false;}
	bool	operator!=(T* pData) { return !(this == pData);}
	bool	operator!=(const RefPtr<T>& pPtr){ return !(pPtr == *this);}

	T*		operator->(){return mRef;}
};

template<class T>
	RefPtr<T>::RefPtr(T* pData)
{
	mRef = pData;
	mComptor = new int(1);
}

template<class T>
	RefPtr<T>::RefPtr(const RefPtr<T>& pPtr)
{
	mRef = pPtr.mRef;
	mComptor = pPtr.mComptor;

	(*mComptor)++;
}

template<class T>
	RefPtr<T>::~RefPtr()
{
	if(mComptor != 0)
	{
		(*mComptor)--;
		if(*mComptor == 0)
		{
			delete mComptor;
			mComptor = 0;
			delete mRef;
		}
	}
}

template <class T>
	RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& pPtr)
{
	mRef = pPtr.mRef;
	mComptor = pPtr.mComptor;

	(*mComptor)++;

	return *this;
}

template <class T>
	RefPtr<T>& RefPtr<T>::operator=(T* pData)
{
	if(mComptor != NULL)
	{
		(*mComptor)--;
		if(mComptor == 0)
		{
			delete mComptor;
			mComptor = NULL;
			delete mRef;
		}
	}

	mComptor = new int(1);
	mRef = pData;

	return *this;
}

#endif